//
//  HWMyWalletView.m
//  嗨文
//
//  Created by 响当当 on 2019/2/19.
//  Copyright © 2019年 dongzheng. All rights reserved.
//

#import "HWMyWalletView.h"
#import "HWMyWalletHeadView.h"
#import "HWMyWalletCellCell.h"
#import "HWLevelViewController.h"

@interface HWMyWalletView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)HWMyWalletHeadView *myWalletHeadView;
@property (nonatomic, strong)UIView *footView;

@property (nonatomic, copy)NSArray *headArr;

@property (nonatomic, strong)NSMutableArray *cellArr;

@property (nonatomic, strong)NSString *segmentStr;
@property (nonatomic, assign)BOOL hasNextPage;
@property (nonatomic, strong)NSString *pageNum;
@end

@implementation HWMyWalletView

- (NSArray *)headArr
{
    if (!_headArr)
    {
        _headArr = [[NSArray alloc]init];
    }
    return _headArr;
}

- (NSMutableArray *)cellArr
{
    if (!_cellArr)
    {
//        _cellArr = @[@{@"title":@"2018-12-26 10:58",@"time":@"2018-12-26 10:58",@"money":@"+96.18",@"isPass":@"1"},
//                     @{@"title":@"2018-12-26 10:58",@"time":@"2018-12-26 10:58",@"money":@"+96.18",@"isPass":@"1"},
//                     @{@"title":@"2018-12-26 10:58",@"time":@"2018-12-26 10:58",@"money":@"+96.18",@"isPass":@"1"}];
        _cellArr = [[NSMutableArray alloc]init];
    }
    return _cellArr;
}

- (UIView *)footView
{
    if (!_footView)
    {

        _footView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.tableView.frame),SCREEN_WIDTH,60*kUIScaleBasePlus)];
        _footView.backgroundColor = LCHexColor(0xFFFFFF);

        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,1)];
        lineView.backgroundColor = LCHexColor(0xEBE5E1);
        [_footView addSubview:lineView];

        UIButton *levelBtn = [HWUtilties creatButtonWithFrame:CGRectMake(18*kUIScaleBasePlus,10*kUIScaleBasePlus,122*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentCenter
                                                         text:@"排行榜"
                                                     textClor:LCHexColor(0xF54337)
                                                    buttonBGC:LCHexColor(0xFDE5E4)
                                                     textFont:18*kUIScaleBasePlus
                                                       target:self
                                                       method:@selector(levelClick)];
        levelBtn.layer.masksToBounds = YES;
        levelBtn.layer.cornerRadius = levelBtn.frame.size.height/2;
        [_footView addSubview:levelBtn];

        UIButton *friendBtn = [HWUtilties creatButtonWithFrame:CGRectMake(CGRectGetMaxX(levelBtn.frame)+19*kUIScaleBasePlus,10*kUIScaleBasePlus,199*kUIScaleBasePlus,40*kUIScaleBasePlus)
                                                 textAligment:NSTextAlignmentCenter
                                                         text:@"拉好友扶我"
                                                     textClor:LCHexColor(0xFFFFFF)
                                                    buttonBGC:LCHexColor(0xF54337)
                                                     textFont:18*kUIScaleBasePlus
                                                       target:self
                                                       method:@selector(friend)];
        friendBtn.layer.masksToBounds = YES;
        friendBtn.layer.cornerRadius = friendBtn.frame.size.height/2;
        [_footView addSubview:friendBtn];

    }
    return _footView;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT-60*kUIScaleBasePlus-TABBAR_HEIGHT-10*kUIScaleBasePlus) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.tableHeaderView = self.myWalletHeadView;
        
        
//        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadUpChapter)];
//
//        // 设置文字
//        [header setTitle:@"我的记录" forState:MJRefreshStateIdle];
//        [header setTitle:@"下拉刷新" forState:MJRefreshStatePulling];
//        [header setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
//
//        // 设置字体
//        header.stateLabel.font = [UIFont systemFontOfSize:12];
//        header.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:10];
//
//        // 设置颜色
//        header.stateLabel.textColor = LCHexColor(0x1f1f1f);
//        header.lastUpdatedTimeLabel.textColor = [UIColor clearColor];
//        // 设置刷新控件
//        self.tableView.mj_header = header;
        
        
        //上拉刷新
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        //设置上拉刷新属性
        [footer setTitle:@"加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"Loading ..." forState:MJRefreshStateRefreshing];
//        [footer setTitle:@"去添加更多喜欢的小说吧" forState:MJRefreshStateNoMoreData];
        
        // 设置字体
        footer.stateLabel.font = [UIFont systemFontOfSize:14*kUIScaleBasePlus];
        
        // 设置颜色
        footer.stateLabel.textColor = JSALineColor;
        
        _tableView.mj_footer = footer;
    }
    return _tableView;
}

- (HWMyWalletHeadView *)myWalletHeadView
{
    if (!_myWalletHeadView)
    {
        _myWalletHeadView = [[HWMyWalletHeadView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,295*kUIScaleBasePlus)];
        WeakSelf(self)
        _myWalletHeadView.getBackSegmentChangeBlock = ^(NSString *num) {
            [SVProgressHUD showWithStatus:@"加载中"];
            [weakSelf.cellArr removeAllObjects];
            weakSelf.pageNum = @"1";
            weakSelf.hasNextPage = YES;
            weakSelf.segmentStr = num;
            if (weakSelf.getBackSegmentChangeBlock)
            {
                weakSelf.getBackSegmentChangeBlock(num);
            }
        };
    }
    return _myWalletHeadView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.hasNextPage = YES;
        self.pageNum = @"1";
        self.segmentStr = @"0";
        
        [self addSubview:self.tableView];
        [self addSubview:self.footView];
    }
    
    return self;
}


#pragma mark - tableView Delegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 68*kUIScaleBasePlus;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    HWMyWalletCellCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyWalletCellCell"];
    if (!cell)
    {
        cell = [[HWMyWalletCellCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyWalletCellCell"];
        cell.selectionStyle = 0;
    }
    
    if ([self.segmentStr isEqualToString:@"0"])
    {
        HWGetGoldModel *model = [[HWGetGoldModel alloc]initWithDic:self.cellArr[indexPath.row]];
        [cell getGoldRecord:model];
    }
    else if([self.segmentStr isEqualToString:@"1"])
    {
        HWGoldExChangeModel *model = [[HWGoldExChangeModel alloc]initWithDic:self.cellArr[indexPath.row]];
        [cell getGoldExChange:model];
    }
    else
    {
        HWMoneyExChangeModel *model = [[HWMoneyExChangeModel alloc]initWithDic:self.cellArr[indexPath.row]];
        [cell getMoneyExChange:model];
    }
    
    return cell;
}

#pragma mark - Method
//排行榜
- (void)levelClick
{
    HWLevelViewController *vc = [[HWLevelViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
}

//拉好友
- (void)friend
{
    if ([current_User.isHideinfo.stringValue isEqualToString:@"0"])
    {
        [HWUtilties pushWebWithUrl:@"http://www.haishuwu.com/sharePage?url=http%3A%2F%2Fwww.haishuwu.com%2FfirstRedBag%3Fp%3D617&type=1" title:@"拉好友" toUrl:@"/sharePage?type=1" toId:@"e0b282a7bfae375e"];
    }
    else
    {
        DISMISS_SVP(1.0f);
        [SVProgressHUD showInfoWithStatus:@"暂未开放"];
        DISMISS_SVP(1.0f);
    }

}

#pragma mark - 获取数据
- (void)getDate:(HWMyWalletModel *)model
{
    self.headArr = @[@{@"currentMoney":model.cash,@"currentCoin":model.goldCoin,@"accumulated":model.profit,@"coinAccumlated":[NSString stringWithFormat:@"昨日金币收益结算: 0金币 = 0元 (结算汇率:%@)",model.ratio]},
                     @[@"注册奖",@"时长奖",@"书币奖",@"VIP奖",@"嗨赚奖",@"嗨赚奖"]];
    [self.myWalletHeadView getDate:self.headArr];
    [self.tableView reloadData];
}

//金币兑换
- (void)getGoldExChange:(NSArray *)arr hasNextPage:(NSNumber *)hasNextPage
{
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage)
        {
            self.tableView.mj_footer.hidden = NO;
            [self.cellArr addObjectsFromArray:arr];
            self.hasNextPage = [hasNextPage boolValue];
            [self.tableView reloadData];
        }
        else
        {
            self.tableView.mj_footer.hidden = YES;
        }
    }
    else
    {
        self.tableView.mj_footer.hidden = YES;
    }
}

//余额提现
- (void)getMoneyExChange:(NSArray *)arr hasNextPage:(NSNumber *)hasNextPage
{
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage)
        {
            self.tableView.mj_footer.hidden = NO;
            [self.cellArr addObjectsFromArray:arr];
            self.hasNextPage = [hasNextPage boolValue];
            [self.tableView reloadData];
        }
        else
        {
            self.tableView.mj_footer.hidden = YES;
        }
        
    }
    else
    {
        self.tableView.mj_footer.hidden = YES;
    }
}

//金币获得
- (void)getGoldRecord:(NSArray *)arr hasNextPage:(NSNumber *)hasNextPage;
{
    [SVProgressHUD dismiss];
    if ([arr isValidArray])
    {
        if (self.hasNextPage)
        {
            self.tableView.mj_footer.hidden = NO;
            [self.cellArr addObjectsFromArray:arr];
            self.hasNextPage = [hasNextPage boolValue];
            [self.tableView reloadData];
        }
        else
        {
            self.tableView.mj_footer.hidden = YES;
        }
        
    }
    else
    {
        self.tableView.mj_footer.hidden = YES;
    }
}

#pragma mark - 下拉刷新
- (void)loadNewData
{
    WeakSelf(self)
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (weakSelf.upLoadIndexBlcok)
        {
            if (self.hasNextPage)
            {
                if ([self.segmentStr isEqualToString:@"0"])
                {
                    NSInteger num = self.pageNum.integerValue + 1;
                    self.upLoadIndexBlcok([NSString stringWithFormat:@"%ld",num],@"10");
                    self.pageNum = [NSString stringWithFormat:@"%ld",num];
                }
                else
                {
                    if (self.goldOrMoneyBlock)
                    {
                        NSInteger num = self.pageNum.integerValue + 1;
                        self.goldOrMoneyBlock(self.segmentStr,[NSString stringWithFormat:@"%ld",num],@"10");
                        self.pageNum = [NSString stringWithFormat:@"%ld",num];
                    }
                }
                
            }
            else
            {
                if ([self.segmentStr isEqualToString:@"0"])
                {
                    self.upLoadIndexBlcok(@"1",@"10");
                }
                else
                {
                    if (self.goldOrMoneyBlock)
                    {
                        self.goldOrMoneyBlock(@"0",@"1",@"10");

                    }
                }
            }
        }
        
        [weakSelf.tableView.mj_footer endRefreshing];
        
    });
}

@end
